package io.renren.sf.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.sf.dao.CbChapterListDao;
import io.renren.sf.entity.*;
import io.renren.sf.service.*;
import io.renren.sf.vo.BookToUnitVo;
import io.renren.sf.vo.ChapterVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.common.utils.R;


/**
 * @author cyl_txw
 * @email cyl_txw@gmail.com
 * @date 2021-03-31 13:04:12
 */
@RestController
@RequestMapping("sf/cbchapter")
public class CbChapterController {
    @Autowired
    private CbChapterListDao cbChapterListDao;
    @Autowired
    private CbChapterService cbChapterService;
    @Autowired
    private CbChapterPictureService cbChapterPictureService;
    @Autowired
    private CbUnitService cbUnitService;
    @Autowired
    private CbTypefaceService cbTypefaceService;
    @Autowired
    private CbContentsService cbContentsService;
    /**
     * 通过id获取unitId
     */
    @GetMapping("/getUnitIdById/{id}")
    public R getUnitIdById(@PathVariable("id") Long id){
        // 先看看是不是unitId
        CbUnitEntity byId = cbUnitService.getById(id);
        if(byId != null){
            // 说明是unitId
            String contents = cbContentsService.getById(byId.getCid()).getContents();
            return R.ok().put("unitId", id).put("contents", contents);
        }else {
            // 是wordId，通过它获取unitId
            Long unitId = cbTypefaceService.getUnitIdById(id);
            String contents = cbContentsService.getById(cbUnitService.getById(unitId).getCid()).getContents();
            return R.ok().put("unitId", unitId).put("contents", contents);
        }
    }
    /**
     * 章节分页
     */
    @GetMapping("/list")
    public R chapterPage(@RequestParam Map<String, Object> params) {
        String uid = (String) params.get("uid");
        List<CbChapterEntity> list = cbChapterService.getChapterAndPicAndWord(Long.parseLong(uid));
        return R.ok().put("chapterList", list);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("sf:cbchapter:info")
    public R info(@PathVariable("id") Long id) {
        CbChapterEntity cbChapter = cbChapterService.getById(id);
        return R.ok().put("cbChapter", cbChapter);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions("sf:cbchapter:save")
    public R save(@RequestBody CbChapterEntity chapterEntity) {
        cbChapterService.save(chapterEntity);
        return R.ok().put("chapter", chapterEntity);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    //@RequiresPermissions("sf:cbchapter:update")
    public R update(@RequestBody CbChapterEntity cbChapter) {
        cbChapterService.updateById(cbChapter);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    //@RequiresPermissions("sf:cbchapter:delete")
    public R delete(@RequestBody Long[] ids) {
        List<Long> chapterIds = Arrays.asList(ids);
        /*// 先删章节图片
        for (Long chapterId : chapterIds) {
            cbChapterPictureService.remove(new QueryWrapper<CbChapterPictureEntity>().eq("cid", chapterId));
        }*/
        cbChapterService.removeByIds(chapterIds);
        return R.ok();
    }

    /**
     * 通过editionId获取book和unit的级联菜单
     */
    @GetMapping("/getBookAndUnitByEditionId/{editionId}")
    //@RequiresPermissions("sf:cbtypeface:delete")
    public R getBookAndChapterByEditionId(@PathVariable("editionId") Long editionId){
        List<BookToUnitVo> list = cbChapterService.getBookAndUnitByEditionId(editionId);
        return R.ok().put("unitList", list);
    }

}
